package com.heima.behavior.service;

import com.alibaba.fastjson.JSON;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.utils.thread.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class BehaviorService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    /**
     * 点赞
     * @param likesBehaviorDto
     * @return
     */
    public ResponseResult likesBehavior(LikesBehaviorDto likesBehaviorDto) {
        //校验参数
        if (likesBehaviorDto==null || likesBehaviorDto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取文章id
        Long articleId = likesBehaviorDto.getArticleId();
        //用户id
        Integer apUserId = AppThreadLocalUtils.getUser().getId();

        //实时更新 存入数据库
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(articleId);
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);

        if (likesBehaviorDto.getOperation()==0){
            //存入redis
            cacheService.hPut(ArticleConstants.USER_LIKES_BEHAVIOR,apUserId+"::"+articleId,"0");

            mess.setAdd(1);

        }else {
            //删除
            cacheService.hDelete(ArticleConstants.USER_LIKES_BEHAVIOR,apUserId+"::"+articleId);

            mess.setAdd(-1);

        }
        //存入数据库
        //kafkaTemplate.send(ArticleConstants.LIKES_BEHAVIOR, JSON.toJSONString(likesBehaviorDto));

        //发送消息，数据聚合
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));

        return ResponseResult.okResult("");
    }


    /**
     * 阅读
     * @param readDto
     * @return
     */
    public ResponseResult readBehavior(ReadDto readDto) {
        //参数校验
        if (readDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Long articleId = readDto.getArticleId();
        //自增
        cacheService.hIncrBy(ArticleConstants.USER_READ_BEHAVIOR,articleId+"",1L);

        //阅读数量写入数据库
        //kafkaTemplate.send(ArticleConstants.READ_BEHAVIOR,readDto.getArticleId().toString());

        //发送消息，数据聚合
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(readDto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        mess.setAdd(1);
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));


        return ResponseResult.okResult("");
    }

    /**
     * 不喜欢/取消不喜欢
     * @param unLikesBehaviorDto
     * @return
     */
    public ResponseResult unLikesBehavior(UnLikesBehaviorDto unLikesBehaviorDto) {
        //参数校验
        if (unLikesBehaviorDto==null || unLikesBehaviorDto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer apUserId = AppThreadLocalUtils.getUser().getId();
        Long articleId = unLikesBehaviorDto.getArticleId();

        //0不喜欢 1取消不喜欢
        if (unLikesBehaviorDto.getType()==0){
            cacheService.hPut(ArticleConstants.USER_UNLIKES_BEHAVIOR,apUserId+"::"+articleId,"0");
        }else {
            cacheService.hDelete(ArticleConstants.USER_UNLIKES_BEHAVIOR,apUserId+"::"+articleId);
        }
        return ResponseResult.okResult("");
    }

}
